﻿#region Using Statements
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using TexturedQuad;
#endregion

namespace kinect_music_shooter.GameWorld
{
    class GameObject2D
    {
        Quad quad;
        VertexDeclaration vertexDeclaration;
        Texture2D texture;
        BasicEffect quadEffect;
        public BoundingSphere boundingSphere;

        public Vector3 position = Vector3.Zero;
        public Vector3 rotation = Vector3.Zero;
        public Vector3 velocity = Vector3.Zero;
        public float scale = 1.0f;


        public GameObject2D(GraphicsDevice pGraphicsDevice)
        {
            quadEffect = new BasicEffect(pGraphicsDevice);
            ////////////////////////////////////////////
            velocity = new Vector3(0, 0, 0);
            boundingSphere.Center = position;
            boundingSphere.Radius = 1f;
            //////////////////////////////////////////////

            vertexDeclaration = new VertexDeclaration(new VertexElement[]
                {
                    new VertexElement(0, VertexElementFormat.Vector3, VertexElementUsage.Position, 0),
                    new VertexElement(12, VertexElementFormat.Vector3, VertexElementUsage.Normal, 0),
                    new VertexElement(24, VertexElementFormat.Vector2, VertexElementUsage.TextureCoordinate, 0)
                }
            );
        }

        public void init(Vector3 origin, Vector3 normal, Vector3 up, float width, float height)
        {
            quad = new Quad(origin, normal, up, width, height);
            boundingSphere.Radius = (width + height) / 2;
        }

        public void LoadTexture(Texture2D pTexture)
        {
            texture = pTexture;
        }

        public void Draw(GraphicsDevice pGraphicsDevice, Camera camera)
        {
            //quadEffect.EnableDefaultLighting();
            quadEffect.World =
                        Matrix.CreateFromYawPitchRoll(
                        rotation.Y,
                        rotation.X,
                        rotation.Z) *

                        Matrix.CreateScale(scale) *

                        Matrix.CreateTranslation(position);
            quadEffect.View = camera.View;
            quadEffect.Projection = camera.Projection;

                quadEffect.TextureEnabled = true;
                quadEffect.Texture = texture;

            //graphicsDevice.Clear(Color.CornflowerBlue);

            foreach (EffectPass pass in quadEffect.CurrentTechnique.Passes)
            {
                pass.Apply();

                pGraphicsDevice.DrawUserIndexedPrimitives
                    <VertexPositionNormalTexture>(
                    PrimitiveType.TriangleList,
                    quad.Vertices, 0, 4,
                    quad.Indexes, 0, 2);
            }
        }
    }
}
